
Yacon.widget.TabPage = function(conText)
{
	this.activedPage = null;
	this.elementID      = "";
	
	conText = conText ||{};
	
	Yacon.apply(this,conText);
	
	this.create();
}

Yacon.widget.TabPage.Extend(Object,{
	create:function()
	{
		this.element            	= document.all[this.elementID];
		if (!this.element)
			return;
	
		this.element.Control    	= this;
		
		this.setActivePage(this.element.getAttribute("activeTab"));
		
		this.setHeight();
		this.setWidth();
		
		 _array_resizeElement.add(this.element);
	},
	setActiveCell:function(activeCell)
	{
		this.activeCell = activeCell;
		
		var activeRow = Yacon.getTR(activeCell);
		
		if (this.activedPage == activeCell) return;			
		
		if (this.activedPage)
		{			
			var prevCell = activeRow.cells[this.activedPage.cellIndex - 1];
			var nextCell = activeRow.cells[this.activedPage.cellIndex + 1];			
			this.activedPage.setAttribute("background",window.contextPath +window.imgPath  +"/tab/tab_button.gif");
						
			if(prevCell)
			{
				if (prevCell.getAttribute("firstcell"))
					prevCell.firstChild.setAttribute("src",window.contextPath +window.imgPath  +"/tab/start_tab.gif");
				else
					prevCell.firstChild.setAttribute("src",window.contextPath +window.imgPath  +"/tab/tab.gif");
			}			
			if(nextCell)
			{		
				if (nextCell.getAttribute("lastcell"))
					nextCell.firstChild.setAttribute("src",window.contextPath +window.imgPath  +"/tab/end_tab.gif");
				else
					nextCell.firstChild.setAttribute("src",window.contextPath +window.imgPath  +"/tab/tab.gif");
			}
			
			if (document.all["sheet_"+this.activedPage.getAttribute("tab_code")])
				document.all["sheet_"+this.activedPage.getAttribute("tab_code")].className="sheetunLoad";
			
			if (document.all["frame_"+this.activedPage.getAttribute("tab_code")])
			{
				if (Yacon.isTrue(this.activedPage.getAttribute("ifReload")))
					document.all["frame_"+this.activedPage.getAttribute("tab_code")].src="";
			}
		}
		
		var prevCell = activeRow.cells[activeCell.cellIndex - 1];
		var nextCell = activeRow.cells[activeCell.cellIndex + 1];		
		activeCell.setAttribute("background",window.contextPath +window.imgPath  +"/tab/tab_active_button.gif");
		
		if(prevCell)
		{
			if (prevCell.getAttribute("firstcell"))
				prevCell.firstChild.setAttribute("src",window.contextPath +window.imgPath  +"/tab/active_start_tab.gif");
			else
				prevCell.firstChild.setAttribute("src",window.contextPath +window.imgPath  +"/tab/active_tab1.gif");
		}
		if(nextCell)
		{		
			if (nextCell.getAttribute("lastcell"))
				nextCell.firstChild.setAttribute("src",window.contextPath +window.imgPath  +"/tab/active_end_tab.gif");
			else
				nextCell.firstChild.setAttribute("src",window.contextPath +window.imgPath  +"/tab/active_tab2.gif");
		}	
		
		if (document.all["sheet_"+activeCell.getAttribute("tab_code")])
			document.all["sheet_"+activeCell.getAttribute("tab_code")].className="sheetLoad";
		
		if (document.all["frame_"+activeCell.getAttribute("tab_code")])
		{
						
			//重置地址
			this.resetUrl();
			
			if (Yacon.isTrue(activeCell.getAttribute("ifReload"))){			
				document.all["frame_"+activeCell.getAttribute("tab_code")].src= this.activeUrl;
			}				
			else if (Yacon.isEmpty(document.all["frame_"+activeCell.getAttribute("tab_code")].src)){
				document.all["frame_"+activeCell.getAttribute("tab_code")].src= this.activeUrl;
			}
				
		}
		this.activedPage = activeCell;
	},
	setActivePage:function(tabpage)
	{
		if (Yacon.isEmpty(tabpage)) return;
		
		for(var i = 0; i < this.element.rows[0].cells.length; i++)
		{		
			if (this.element.rows[0].cells[i].getAttribute("tab_code") == tabpage){			
				this.setActiveCell(this.element.rows[0].cells[i]);
				break;
			}
		}
	},
	setActiveIndex:function(index)
	{
		if (Yacon.getInt(index)<0) return;
		
		for(var i = 0; i < this.element.rows[0].cells.length; i++)
		{		
			if (this.element.rows[0].cells[i].getAttribute("tab_index") == index){			
				this.setActiveCell(this.element.rows[0].cells[i]);
				break;
			}
		}
	},
	setHeight:function(height)
	{
		if (!height)
		{
			var absPos = Yacon.getAbsPosition(this.element);
			height = document.body.clientHeight- absPos[1]-5;
		}
			
		this.element.style.height = height+"px";
		if (document.all["content_"+this.elementID])
			document.all["content_"+this.elementID].style.height = (this.element.offsetHeight-25)+"px";		
	},
	setWidth:function(width)
	{
		if (!width)
			width = document.body.offsetWidth;
		
		width = width - 8;
		this.element.style.width = width +"px";				
	},
	setVisible:function(tabID)
	{
			
	},
	setDisabled:function(tabID)
	{
			
	},
	setPageUrl:function(tabID,url)
	{
			
	},
	getPageFrame:function(tabID)
	{
			
	},
	resetUrl:function(){
		var activeUrl = this.activeCell.getAttribute("tab_url");
		if(activeUrl){
			if(activeUrl.trim().startsWith("http")){
				
			}else{
				activeUrl = window.contextPath + activeUrl;
			}
		}
		
		//补充参数处理
		if(activeUrl.contains("?")){}
		else{
			activeUrl += "?A=1";
		}
					
		//添加字典参数
		var dictParams = Yacon.getUrlParam("params", activeUrl.search());			
		if(dictParams){
			var params = dictParams.split(",");
			for(var i=0;i<params.length;i++){
				var value = "";				
				for(var j=0;j<_array_dataset.length;j++){
					value = _array_dataset[j].getFieldValue(params[i]);
					if(value && value != ""){
						break;
					}
				}
				
				activeUrl += "&"+params[i]+"="+value;
			}
		}
		
		activeUrl += Yacon.getCommonParam();
		
		this.activeUrl = activeUrl;
		
		//添加额外参数
		if (Yacon.isfireuserEvent(this.elementID + "_afterParam"))
		{
			Yacon.fireUserEvent(this.elementID + "_afterParam",[this]);
				
		}
	},
	refresh:function(){
		//重置地址
		this.resetUrl();
		
		if (Yacon.isTrue(this.activeCell.getAttribute("ifReload"))){			
			document.all["frame_"+this.activeCell.getAttribute("tab_code")].src= this.activeUrl;
		}				
		else if (Yacon.isEmpty(document.all["frame_"+this.activeCell.getAttribute("tab_code")].src)){
			document.all["frame_"+this.activeCell.getAttribute("tab_code")].src= this.activeUrl;
		}
	}
});

/**
标签组件外部单击事件
参数：参数：divobj 事件发生对象
**/
function tabpage_onclick(divobj)
{
	var tabObj = Yacon.getTable(divobj);	
	tabObj.Control.setActivePage(divobj.getAttribute("id"));
}
